<?php

namespace app\api\logic;
use app\common\basics\Logic;
use think\Db;


/**
 * 工作台统计
 * Class StatLogic
 * @package app\admin\logic\index
 */
class QuanLogic extends Logic
{
    public static function give_quan($user_id,$city_shi){

        $coupon_list = Db::name('coupon')->where('((expire_type = 0) or (expire_type = 1 and fixed_time_end > '.time().')) and send_scene = 1 and is_enable = 1 and is_delete_time = 0')->where('send_proxy','<>','')->where('send_scene',1)->select();
        $max_num = Db::name('coupon')->where('((expire_type = 0) or (expire_type = 1 and fixed_time_end > '.time().')) and send_scene = 1 and is_enable = 1 and is_delete_time = 0')->where('send_proxy','<>','')->where('send_scene',1)->sum('one_send_count');
        $user_quan_list = Db::name('coupon_user')->where('user_id',$user_id)->column('coupon_id');
        if(!empty($coupon_list))
        {
            //用户优惠券数据
            $coupon_user_data = [];
//            循环优惠券列表
            foreach($coupon_list as $key => $value)
            {
                $city = json_decode($value['send_proxy']);
                //获取用户定位城市id
                $city_id = Db::name('proxy')->where('shi',$city_shi)->value('proxy_id');
                $user_info = Db::name('user')->where('id',$user_id)->find();
                $buy_history = Db::name('order')->where('user_id',$user_info['id'])->find();
                if (in_array($city_id,$city) && time() - $user_info['add_time'] < 18000 && empty($buy_history) || $user_id == 6879 && in_array($city_id,$city)){
                    if(($value['limit_send_count'] == 0) || ($value['limit_send_count'] > 0 && $value['already_send_count'] < $value['limit_send_count']))
                    {
                        //单次发放数量
                        $one_send_count = $value['one_send_count'];
                        if($value['limit_send_count'] > 0)
                        {
                            $surplus = $value['limit_send_count']-$value['already_send_count'];
                            $one_send_count = $surplus >= $value['one_send_count'] ? $value['one_send_count'] : $surplus;
                        }
                        //过期时间计算
                        switch($value['expire_type'])
                        {
                            // 领取生效
                            case 0 :
                                $time_start = time();
                                $time_end = time()+(3600*$value['expire_hour']);
                                break;
                            // 固定日期
                            case 1 :
                                $time_start = $value['fixed_time_start'];
                                $time_end = $value['fixed_time_end'];
                                break;
                        }
                        for($i = 0; $i < $one_send_count; $i++)
                        {
                            if (in_array($value['id'],$user_quan_list)){
                                continue;
                            }
                            $coupon_user_data[] = [
                                'user_id'           => $user_id,
                                'coupon_id'         => $value['id'],
                                'name'              => $value['name'],
                                'coupon_type'       => $value['coupon_type'],
                                'discount_value'    => $value['discount_value'],
                                'where_order_price' => $value['where_order_price'],
                                'time_start'        => $time_start,
                                'time_end'          => $time_end,
                                'is_valid'          => 1,
                                'add_time'          => time(),
                                'upd_time'          => time()
                            ];
                        }
                    }
                }

            }
            if(!empty($coupon_user_data))
            {
                $add_num = count($coupon_user_data);

                // 开启事务
                Db::startTrans();
                //用户添加优惠券
                $coupon_user_id = Db::name('coupon_user')->insertAll($coupon_user_data);

                foreach ($coupon_user_data as $value){
                    //更新优惠券已发放数量
                    if(!Db::name('coupon')->where(['id' => $value['coupon_id']])->update(['already_send_count' => Db::raw('already_send_count+1')]))
                    {
                        // 事务回滚
                    }
                }

                // 提交事务
                Db::commit();
                $result['msg'] = '您的优惠券已到账';
                $result['code'] = 0;
            }else{
                $result['msg'] = '请求成功';
                $result['code'] = -1;
            }
        }else{
            $result['msg'] = '暂无优惠券';
            $result['code'] = -1;
        }

        return $result;

    }


}